let _ = GMain.init ()


let img_adress = ref "~/projetspe/image"
let file = ref false
let map_pretreat = ref false
let displayed = ref false
let state = ref 0
let step = ref 1
let b = ref true


(*******************************************************************)
(*******************************************************************)
(*****		         LES FENETRES       		       *****)
(*******************************************************************)
(*******************************************************************)

let window =
	let wind = GWindow.window
		~position:`CENTER
		~resizable:false
		~height:720
		~width:1100
		~title:"aquila" () in
		wind#connect#destroy GMain.quit;
		wind

(*******************************************************************)
(*******************************************************************)
(*****			LES CONTENEURS 	                       *****)
(*******************************************************************)
(*******************************************************************)


	(*Le Père tout puissant*)
let container = GPack.vbox
	~spacing:10
	~border_width:16
	~packing:window#add () 


let create_menu () =
  let file_menu = GMenu.menu () in
  let item = GMenu.menu_item 
    ~label:"Restart"
    ~packing:file_menu#append () in
  let item2 = GMenu.menu_item 
    ~label:"Other"
    ~packing:file_menu#append () in
  let item2 = GMenu.menu_item 
    ~label:"Exit"
    ~packing:file_menu#append () in
  GMain.init ();
  item2#connect#activate ~callback:GMain.quit;
	file_menu

let create_edit () =
  let edit_menu = GMenu.menu () in
  let item = GMenu.menu_item 
    ~label:"Language" 
    ~packing:edit_menu#append () in
  GMain.init ();
 (* item#connect#activate ~callback:(fun () -> popup_step ();());*)
  (*Donne en parametre mkobj carte2 carte1 lepas +valeurs des hauteurs*)
	edit_menu

let create_quit () =
  let quit_menu = GMenu.menu ()in
  let item = GMenu.menu_item 
    ~label:"Online help" 
    ~packing:quit_menu#append () in
	GMain.init();
  item#connect#activate ~callback:(fun () -> Sys.command "firefox http://www.aquila.fr/help/";());
   quit_menu



let menu =
  let file_menu = create_menu () in
  let edit_menu = create_edit () in
  let quit_menu = create_quit () in
  let menu_bar = GMenu.menu_bar 
    ~packing:container#pack () in
  let file_item = GMenu.menu_item 
    ~label:"File" () in
  let edit_item = GMenu.menu_item 
    ~label:"Edit" () in
  let quit_item = GMenu.menu_item 
    ~label:"Help"() in
file_item#set_submenu file_menu;
edit_item#set_submenu edit_menu;
quit_item#set_submenu quit_menu;
menu_bar#append file_item;
menu_bar#append edit_item;
menu_bar#append quit_item



(* box du haut contenant le graphe et le différentes box de fonction mathematiques*)
let fenetre_du_haut = GPack.hbox 
	~spacing:10
	~border_width:16
	~packing:container#add () 
(*box du bas contenant les deux champs pouvant etre remplis champs de fonction et champs de calcul mathématiques*)
let fenetre_du_bas = GPack.hbox 
	~spacing:10
	~border_width:16
	~packing:container#add () 
(*
let fdb_carre_gauche = GPack.vbox 
	~spacing:20
	~border_width:25
	~packing:fenetre_du_bas#add () 
*)
(*let fdb_carre_droite = GPack.vbox 
	~spacing:20
	~border_width:25
	~packing:fenetre_du_bas#add () 
*)


(*
	(*Pour l'image*)
let image_box =  GPack.button_box `HORIZONTAL
	~layout:`SPREAD
	~packing:fenetre_du_haut#add () 
*)
	(*Pour l'image*)
let image_box =  GPack.button_box `HORIZONTAL
	~layout:`SPREAD
	~packing:(fenetre_du_haut#pack ~expand:false) ()

(* gpack vbox*)

let img1 = GMisc.image
	~file:"image.jpg"
	~height:350
	~packing:image_box#add ()

(*
let pixbuf_ok_sel image width height =
	let pixbuf = GdkPixbuf.from_file_at_size (!file_path)
		~width:width
		~height:height
	in
	image#set_pixbuf pixbuf
*)	
(*
let btn = GButton.button  
	~image_position:img_adress 
	~packing:fenetre_du_haut#add () 
  GMisc.image  
	~packing:btn#set_image ()
*)

(* Insertion de barres de défilement. *)
let scroll = GBin.scrolled_window
  ~height:200
  ~hpolicy:`ALWAYS
  ~vpolicy:`ALWAYS
  ~packing:fenetre_du_haut#add ()


(* Conteneur en forme de tableau. La méthode add_with_viewport permet d'insérer
 * un conteneur tel que GPack.table (GtkTable) en le faisant défiler à l'aide 
 * des barres de défilement de GBin.scrolled_window (GtkScrolledWindow). *)
(*let table = GPack.table
  ~row_spacings:5
  ~col_spacings:5
  ~homogeneous:true
  ~packing:scroll#add_with_viewport ()*)

let bbox2 = GPack.button_box `VERTICAL
	~layout:`SPREAD
	~packing:(scroll#add_with_viewport ) ()

let bbox3 = GPack.button_box `HORIZONTAL
	~layout:`SPREAD
	~packing:(bbox2#add ) ()
	
let bbox4 = GPack.button_box `HORIZONTAL
	~layout:`SPREAD
	~packing:(bbox2#add ) ()

let bov =
	for i=0 to 8 do
		begin
		let bbox5 = GPack.button_box `HORIZONTAL
		~layout:`SPREAD
		~packing:(bbox2#add ) () in
		bbox5
		end
	done



let bbox6 = GPack.button_box `HORIZONTAL
	~layout:`SPREAD
	~packing:(bbox2#add ) ()

let bbox7 = GPack.button_box `HORIZONTAL
	~layout:`SPREAD
	~packing:(bbox2#add ) ()



let bbox8 = GPack.button_box `HORIZONTAL
	~layout:`SPREAD
	~packing:(bbox2#add ) ()

  (*Conteneur pour les boutons*)
let bbox = GPack.button_box `HORIZONTAL
	~layout:`SPREAD
	~packing:(container#pack ~expand:false) ()
(*
let bbox2 = GPack.button_box `HORIZONTAL
	~layout:`SPREAD
	~packing:(table#attach (GMisc.label (*~markup*) ~packing:bbox2#add ())) ()
*)
(*
let entry = GEdit.entry ~text ~width:80 ~packing:(attach i) () in
    entry#misc#modify_base [`NORMAL, `NAME text]
*)
let bot_box = GPack.button_box `HORIZONTAL
	~layout:`SPREAD
	~packing:(container#pack ~expand:false) ()

let frame_trace = GBin.frame
		~border_width:10
		~label:"trace"
		~packing:fenetre_du_bas#add ()

let fdb_carre_gauche = GPack.vbox 
	~spacing:20
	~border_width:25
	~packing:frame_trace#add () 

let frame_derive = GBin.frame
		~border_width:10
		~label:"derive"
		~packing:fenetre_du_bas#add ()




let fdb_carre_droite = GPack.vbox 
	~spacing:20
	~border_width:25
	~packing:frame_derive#add () 



let trace_function= GEdit.entry ~text:"2x²+5x-27" ~max_length:500 ~packing:fdb_carre_gauche#add () 

let derive_function= GEdit.entry ~text:"ln(6x² -8x³)" ~max_length:500 ~packing:fdb_carre_droite#add ()


(*
let vbox2 = GPack.vbox ~spacing:5 ~packing:fdb_carre_droite#add () 

let frame = GBin_.frame ~label:"Normal Label" ~packing:vbox2#pack () in_
  GMisc.label ~text:"This is a normal label" ~packing:frame#add ();
*)


(*
let frame_trace = GBin.frame
		~border_width:10
		~label:"trace"
		~packing:fenetre_du_bas#add ()

let vbox2 = GPack.vbox ~spacing:5 ~packing:fdb_carre_droite#add () 
*)
(*
let label_derive = GBin.frame ~label:"" ~packing: vbox2#pack () in
	GMisc.label
	~text:"deriver" ~line_wrap:true ~justify:`FILL ~packing:fdb_carre_droite#add ();
*)
(*
let v_box = GData.adjustment
			  ~lower:0.0 
			 	~upper:500.0
			  ~value:1.
			  ~page_incr:5.0
			  ~step_incr:1.0 
			  ~page_size:0.0 () e
  
let spin = GEdit.entry
    ~adjustment:v_box
let frame_trace = GBin.frame
		~border_width:10
let frame_trace = GBin.frame
		~border_width:10

let trace_function= GEdit.entry ~text:"2x²+5x-27" ~max_length:500 ~packing:fdb_carre_gauche#add () 

let derive_function= GEdit.entry ~text:"ln(6x² -8x³)" ~max_length:500 ~packing:fdb_carre_droite#add ()
		~label:"trace"
		~packing:fenetre_du_bas#add ()

		~label:"trace"
		~packing:fenetre_du_bas#add ()

    ~rate:0.0 ~digits:0 ~wrap:false
    ~numeric:false
		~update_policy:`ALWAYS
    ~packing:fdb_carre_gauche#add ()
*)






(*
(* Un tableau de symboles obtenus grâce à leur code UTF-8. *)
				let symbols =
  Array.concat [
    (* Lettres grecques minuscules. *)
    Array.init  25 (fun i -> Glib.Utf8.from_unichar (i +  945));
    (* Divers symboles mathématiques. *)
    Array.init 256 (fun i -> Glib.Utf8.from_unichar (i + 8704));
  ]
*)

(*
let symbols =Array.concat[Array.init 2 "cos()";Array.init 4 "sin()"]
*)
(*
let init_table ()=
	Array.iteri(let button = GButton.button
								~relief:`NONE
								~packing:table#attack ~left:5 ~top:5)
(**)
(* Le conteneur GPack.table (GtkTable) est rempli : chaque case reçoit un bouton
 * contenant un symbole du tableau < symbols > défini ci-dessus. Le symbole est
 * inséré dans une étiquette (GtkLabel) pour pouvoir utiliser les balises Pango
 * (notamment <big> et </big> qui augmentent la taille du texte). *)
let init_table () =
  Array.iteri (fun i sym ->
    let button = GButton.button 
      ~relief:`NONE 
      ~packing:(table#attach ~left:(i mod 10) ~top:(i / 10)) ()
    and markup = Printf.sprintf "<big>%s</big>" sym in
    ignore (GMisc.label ~markup ~packing_:button#add ())
  ) symbols
*)


(*******************************************************************)
(*******************************************************************)
(*****				LES FONCTIONS                  *****)
(*******************************************************************)
(*******************************************************************)

let button_infos =
	let msg = GWindow.about_dialog
		~parent:window
		~position:`CENTER_ON_PARENT
		~version:"0.2"
		~name: "Aquila"
		~authors:[" ***-> Team Chrysaetos <-*** ";"Romain Coulon";"Kevin Kubarynka";"Alexis Veber";"ludel Ikouna"]
		~copyright:"Copyright © 2011 Aquila"
		~website:"http://www.Aquila.fr/"
		~website_label:"Aquila website"
		~destroy_with_parent:true () in
	let button = GButton.button 	
		~stock:`ABOUT 
		~packing:bot_box#add () in
	GMisc.image 
	~stock:`ABOUT 
	~packing:button#set_image ();
    button#connect#clicked (fun () -> ignore (msg#run ()); msg#misc#hide ());
    button
  



let areusure _ =
	let msg = GWindow.message_dialog
		~parent:window
		~position:`CENTER_ON_PARENT
		~destroy_with_parent:true
		~use_markup:true
		~message_type:`QUESTION
		~message:"\n<b><big>Êtes-vous sur de vouloir quitter ?</big></b>"
		~buttons:GWindow.Buttons.yes_no () in
	let res = msg#run () = `NO in
    msg#destroy ();
    res


let tab = [|"sin()";"cos()";"e⁽⁾";"ln()";"dx";"2x";"arctan()";"ln(8x)";"7x²";"tan()"|]


(*******************************************************************)
(*******************************************************************)
(*****			LES BOUTONS			       *****)
(*******************************************************************)
(*******************************************************************)

let init_table ()=
	begin
	for i=0 to 4 do
	begin	
			let button = GButton.button
				~label:tab.(i)
				~packing:bbox3#add () in
			(*	button#connect#clicked ~callback:GMain.quit;*)
			button
	end
	done;
	for i=5 to 9 do
	begin	
			let button = GButton.button
				~label:tab.(i)
				~packing:bbox4#add () in
				(*button#connect#clicked ~callback:(fun () -> trace_function#text#set "yahoo" ;());*)
			button
	end
	done
	end




(*			
let button =
	let button2 = GButton.button
				~label:"cos()"
				~packing:table#add () in
				button2#connect#clicked ~callback:GMain.quit;
			button2
*)

(*
let button_sin =
	let button_sin2 = GButton.button
		~label:"sin()"
		~packing:table#add () in
	(*button_trace#connect#clicked ~callback:GMain.quit;*)
	button_sin2
*)

let trace =	
	let button_trace = GButton.button
		~label:"trace"
		~packing:fdb_carre_gauche#add () in
	(*button_trace#connect#clicked ~callback:GMain.quit;*)
	button_trace

let integrer =
	let button_integrer = GButton.button
		~label:"resolve"
		~packing:fdb_carre_droite#add () in
	(*button_integrer#connect#clicked ~callback:GMain.quit;*)
	button_integrer

let quit =
	let button = GButton.button
		~stock:`QUIT
		~packing:bot_box#add () in
	   button#connect#clicked ~callback:GMain.quit;
		button

	

let _ =
	window#event#connect#delete ~callback:areusure;
  if (!b) then
(
	b := false;
	init_table ()
);
	window#show ();
  GMain.main ()
(* END *)
